const Generator = require('yeoman-generator')
const fs = require('fs')
const path = require('path')

module.exports = class extends Generator {
  // 配置脚手架问题
  prompting() {
    return this.prompt([
      {
        type: 'input', // 问题的方式
        name: 'name', // 问题结果返回的字段名
        message: 'Your project name', // 面板展示的问题信息
        default: this.appname // 默认值
      }
    ])
      .then(answers => { // answers为输入完信息之后每个字段名对应的信息
        this.answers = answers
      })
  }

  // 写入文件时的操作
  writing() {
    const tmpDir = path.join(__dirname, 'templates/src')
    fs.readdir(tmpDir, (err, files) => {
      if (err) throw err
      console.log(files);
    })
    const files = [
      '.env',
      '.env.development',
      '.env.production',
      '.eslintrc.js',
      // '.gitignore',
      '.prettierrc',
      // '.vscode',
      'index.html',
      'package-lock.json',
      'package.json',
      'postcss.config.js',
      'public/favicon.ico',
      'README.md',
      'vite.config.js',
      'plopfile.js',
      'plop-templates/component.vue.hbs',
      'src/main.js',
      'src/App.vue',
      'src/main.js',
      'src/api/index.js',
      'src/api/Home_Api.js',
      'src/assets/logo.png',
      'src/components/index.js',
      'src/components/LayoutFooter/index.vue',
      'src/router/index.js',
      'src/store/index.js',
      'src/store/modules/user/index.js',
      'src/store/modules/user/actions.js',
      'src/store/modules/user/getters.js',
      'src/store/modules/user/mutations.js',
      'src/store/modules/user/state.js',
      'src/style/base.css',
      'src/utils/dealRequest.js',
      'src/utils/request.js',
      'src/views/Home/api/index.js',
      'src/views/Home/components/Header.vue',
      'src/views/Home/index.vue',
      'src/views/NotFound/index.vue',
    ]
    files.forEach(filePath => {
      this.fs.copyTpl(
        this.templatePath(filePath), // 源路径文件
        this.destinationPath(filePath), // 目标路径文件
        this.answers
      )
    })
  }
}